package com.jiwei.community.controller;

import com.jiwei.community.dto.AddPostDto;
import com.jiwei.community.dto.UpdateMyPostDto;
import com.jiwei.community.entity.Post;
import com.jiwei.community.enums.AppHttpCodeEnum;
import com.jiwei.community.exception.SystemException;
import com.jiwei.community.service.CommunityService;
import com.jiwei.community.service.PostService;
import com.jiwei.community.service.RecommendService;
import com.jiwei.community.utils.BeanCopyUtils;
import com.jiwei.community.utils.SecurityUtils;
import com.jiwei.community.vo.PageListVo;
import com.jiwei.community.vo.PostVo;
import com.jiwei.community.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 季伟
 * @date 2024/3/29
 */
@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    PostService postService;
    @Autowired
    CommunityService communityService;
    @Autowired
    RecommendService recommendService;
    /**
     * 根据传参新增帖子，并且将新帖子的信息返回
     * @param addPostDto
     * @return
     */
    @PostMapping("")
    public ResponseResult publishPost(@RequestBody AddPostDto addPostDto){
        if(addPostDto.getCommunityId() == null)return ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"社区id不能为空");
        return postService.publish(addPostDto);
    }
    @PutMapping("")
    public ResponseResult updateMyPost(@RequestBody UpdateMyPostDto updateMyPostDto){
        Post post = BeanCopyUtils.copyBean(updateMyPostDto,Post.class);
        Long userId = SecurityUtils.getUserId();
        return postService.updateMyPost(userId,post);
    }
    @DeleteMapping("")
    public ResponseResult deleteMyPost(Long postId){
        Long userId = SecurityUtils.getUserId();

        return postService.deleteMyPost(userId,postId);
    }

    @GetMapping("/list")
    public ResponseResult getPostList(Integer pageNum,Integer pageSize,Long communityId,Integer sectionId,Integer isTop,Integer isEssence){
       return postService.getPostList(pageNum,pageSize,communityId,sectionId,isTop,isEssence);
    }

    @PutMapping("/like/{postId}")
    public ResponseResult likePost(@PathVariable Long postId){
       return postService.likePost(postId,SecurityUtils.getUserId());
    }
    @DeleteMapping("/like/cancel/{postId}")
    public ResponseResult cancelLike(@PathVariable Long postId){
        return postService.cancelLike(postId,SecurityUtils.getUserId());
    }
    @GetMapping("/isLiked")
    public ResponseResult isLiked(Long postId){
        Long userId = null;
        try{
            userId = SecurityUtils.getUserId();
        }catch (SystemException e){

        }
        if (userId == null){
            Map map = new HashMap<>();
            map.put("isLiked",false);
            return ResponseResult.okResult(map);
        }else{
            return postService.isLiked(userId,postId);
        }

    }

    @GetMapping("/detail/{postId}")
    public ResponseResult getDetailPost(@PathVariable Long postId){
        return postService.getPostDetail(postId);
    }
    @PutMapping("/viewCount")
    public ResponseResult updateViewCount(Long postId){
        return postService.updateViewCount(postId);
    }
    @GetMapping("/recommend")
    public ResponseResult getRecommendList(Integer pageNum,Integer pageSize){
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        }catch (Exception ignored){}

        PageListVo<PostVo> postPageListVo = recommendService.getRecommendPosts(pageNum,pageSize,userId);
        return ResponseResult.okResult(postPageListVo);
    }
    @GetMapping("/search")
    public ResponseResult searchPost(String title,Long communityId){
        return postService.searchPost(title,communityId);
    }
    @GetMapping("/isMy")
    public ResponseResult isMyPost(Long postId){
        Long userId = null;
        Map<String,Object> resultData = new HashMap<>();
        try{
            userId = SecurityUtils.getUserId();
        }catch (SystemException e){

        }
        if (userId == null){
            resultData.put("isMy",false);
            return ResponseResult.okResult(resultData);
        }
        if (postId == null){
            resultData.put("isMy",false);
            return ResponseResult.okResult(resultData);
        }
        return postService.isMyPost(userId,postId);
    }
    @PostMapping("/reward")
    public ResponseResult rewardPost(Long postId,Integer integral){
        Long userId = SecurityUtils.getUserId();
        return postService.rewardPost(userId,postId,integral);
    }
    @GetMapping("/myList")
    public ResponseResult myPostList(Integer pageNum,Integer pageSize){
        Long userId = SecurityUtils.getUserId();
        return postService.getMyPostList(userId,pageNum,pageSize);
    }

}
